﻿#include "rolecontroller.h"

#include <QJsonDocument>
#include <responesutils.h>
#include <roleservice.h>

RoleController::RoleController(QObject *parent) : HttpController(parent)
{

}

RoleController::~RoleController()
{

}

void RoleController::route(QString path, QString method, HttpRequest &request, HttpResponse &response)
{
    if (method == "GET")
    {
        if (path == "list")
        {
            return getList(request, response);
        }
    }
    else if (method == "POST")
    {
        if (path.isEmpty())
        {
            return saveOne(request, response);
        }
    }
    else if (method == "DELETE")
    {
        return deleteOne(path.toULongLong(), response);
    }
    else if (method == "PUT")
    {
        return saveOne(request, response,path.toULongLong());
    }
}

void RoleController::getList(HttpRequest &request, HttpResponse &response)
{
    uint64_t page = request.getParameter("page").toULongLong();
    uint64_t pageSize = request.getParameter("pageSize").toULongLong();
    RoleService objectService;
    QList<Role> list;
    if(page == 0 || pageSize == 0){
        if(!objectService.getList(list)){
              return ResponesUtils::erroResponse(response,"数据库异常！");
        }
        QJsonArray jsonArray ;
        for(Role &item:list){
            jsonArray.append(item.toJsonObject());
        }
        return ResponesUtils::resultResponse(response,jsonArray);
    }else{
        uint64_t total=0;
        if(!objectService.getPageList(total,list,page,pageSize)){
              return ResponesUtils::erroResponse(response,"数据库异常！");
        }
        QJsonArray jsonArray ;
        for(Role &item:list){
            jsonArray.append(item.toJsonObject());
        }
        return ResponesUtils::pagedResultResponse(response,total,jsonArray);
    }
}

void RoleController::saveOne(HttpRequest &request, HttpResponse &response, uint64_t id)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }
    qDebug()<< __FUNCTION__ << jsonDocument.object();
    Role object;
    object.fromJsonObject(jsonDocument.object());
    if(id != 0){
        object.setId(id);
    }
    qDebug()<< __FUNCTION__ << object.toJsonObject();
    RoleService objectService;
    if(!objectService.saveOne(object,QBaseEntity::CustomProperty,jsonDocument.object().keys())){
        return ResponesUtils::erroResponse(response,"添加失败！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,id == 0 ? "添加成功！" : "修改成功！" );
}

void RoleController::deleteOne(uint64_t id, HttpResponse &response)
{
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    Role object;
    object.setId(id);
    RoleService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}
